<template>
	<view class="container user-feedbackDetail">
		<view class="content-panel yuan-form">
			<view class="content-title">
				<view>意见反馈</view>
				<view class="content-title-num">{{formData.content?formData.content.length:0}}/200</view>
			</view>

			<textarea v-model="formData.content" class="yuan-textarea info-textarea" :maxlength="200"
				placeholder-class="placeholder-style" placeholder="请详述您的问题，以便我们为您提供更好的服务"></textarea>

			<view class="img-panel">
				<template v-if="selHeadInfo.length>0">
					<view class="photo-img-box" v-for="(item,index) in selHeadInfo" :key="item">
						<view class="delete-img" @click="removePhoto(index)">移除</view>
						<image class="photo-img" :src="item" mode="aspectFit" @click="$lookImage(index,selHeadInfo)">
						</image>
					</view>

				</template>
				<view class="photo-img-box add-img-box" @click="editPhoto" v-if="selHeadInfo.length<maxLen">
					<text class="iconfont add-ico">&#xe607;</text>
				</view>
			</view>
		</view>
		<view class="content-panel yuan-form">
			<view class="yuan-form-list">
				<view class="yuan-form-label yuan-label-must">联系方式:</view>
				<input class="yuan-input" v-model="formData.phone" type="number" placeholder="请输入联系方式"
					placeholder-class="placeholder-style">
			</view>
		</view>
		<view class="yuan-btn btn-success bottom-btn" @click="clickSure">提交</view>
	</view>
</template>

<script setup lang="ts">
	import { onMounted, reactive, ref } from 'vue';
	import { onLoad } from "@dcloudio/uni-app"
	import { chooseImage, hideLoading, showLoading, showToast, uploadFile } from '@/common/util';
	import api from '@/common/api';
	const pageType = ref('')
	const maxLen = ref(1)
	const formData = reactive({ content: '', phone: '' })
	const selHeadInfo = ref([])
	onLoad((options) => {
		pageType.value = options.type;
	})
	onMounted(async () => {
		console.info(pageType.value)
		uni.setNavigationBarTitle({
			title: pageType.value === 'new' ? '新功能建议' : "服务投诉"
		});
	})
	const removePhoto = (idx : number) => {
		selHeadInfo.value.splice(idx, 1)
	}
	const editPhoto = () => {
		if (selHeadInfo.value.length >= maxLen.value) {
			uni.showToast({
				title: `最多只能上传${maxLen.value}张图片`,
				duration: 2000
			});
			return
		}
		chooseImage((res) => {
			selHeadInfo.value.push(res.tempFilePaths[0] + '')
		})

	}
	const clickSure = async () => {
		if (!formData.phone) {
			showToast('请输入联系方式')
			return
		}
		if (!formData.content) {
			showToast('请输入反馈内容')
			return
		}
		showLoading()
		const imgRes : any = await uploadFile(selHeadInfo.value[0])
		const data : any = JSON.parse(imgRes.data)
		try {
			const res = await api.addFeedback({
				...formData,
				image: data.url || '',
				type: pageType.value === 'new' ? '1' : "4",
				status: 1
			})
			showToast('保存成功')
			uni.navigateBack()
		} finally {
			hideLoading()
		}
	}
</script>

<style lang="scss">
	.user-feedbackDetail {
		padding: 30rpx;

		.content-panel {
			background: #FFFFFF;
			box-shadow: 0px 8rpx 10rpx 0px rgba(0, 0, 0, 0.1);
			border-radius: 10rpx;
			padding: 20rpx;
			margin-bottom: 20rpx;
		}

		.content-title {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-weight: bold;
			font-size: 34rpx;
			color: #232323;
		}

		.content-title-num {
			font-size: 26rpx;
			font-weight: lighter;
			color: #5B5B5B;
		}

		.info-textarea {
			font-size: 26rpx;
			margin-top: 20rpx;
		}

		.img-panel {
			margin-top: 20rpx;
		}

		.photo-img-box {
			background-color: #eee;
			display: inline-block;
			width: 200rpx;
			height: 200rpx;
			vertical-align: top;
			text-align: center;
			border-radius: 10rpx;
			position: relative;
			margin-right: 20rpx;
			margin-bottom: 20rpx;
		}

		.photo-img-box:nth-child(3n) {
			margin-right: 0;
		}

		.add-img-box {
			line-height: 200rpx;
		}

		.add-ico {
			color: #CBCBCB;
		}

		.delete-img {
			position: absolute;
			top: 0;
			right: 0;
			background-color: rgba(0, 0, 0, 0.5);
			font-size: 22rpx;
			padding: 2rpx 10rpx;
			color: #fff;
			border-radius: 4rpx;
			z-index: 10;
		}

		.photo-img {
			width: 100%;
			height: 100%;
			display: block;
		}

		.bottom-btn {
			height: 88rpx;
			border-radius: 88rpx;
			margin-top: 150rpx;
		}

	}
</style>